//////////////////////////////////////////////////////////////////////////

#ifndef _STATISTICS_H_
#include "statistics.h"
#endif

//////////////////////////////////////////////////////////////////////////

#ifndef _INPUT_H_
#include "input.h"
#endif

#ifndef _TEXTSURFACE_H_
#include "textsurface.h"
#endif

#include <SDL\SDL.h>
#include <boost/lexical_cast.hpp>

//////////////////////////////////////////////////////////////////////////


GUI_Statistics::GUI_Statistics()
{
    m_StatisticsRecieved = false;
}


//////////////////////////////////////////////////////////////////////////


GUI_Statistics::~GUI_Statistics()
{

}


//////////////////////////////////////////////////////////////////////////


bool GUI_Statistics::Render(SDL_Surface *_mainSurface, bool _repaint)
{
    GUI_TextSurface *TextSurface;
    TextSurface = new GUI_TextSurface(GUI_Fonts::CaptionText, GUI_Colors::ButtonText, "Statistics");
    TextSurface->Blt(_mainSurface, 0.2, 0.2);
    delete TextSurface;

    TextSurface = new GUI_TextSurface(GUI_Fonts::ButtonText, GUI_Colors::ButtonText, "Nick");
    TextSurface->Blt(_mainSurface, 0.1, 0.3, GUI_Surface::AllignLeft, 0, 0);
    TextSurface->SetText("Kills");
    TextSurface->Blt(_mainSurface, 0.4, 0.3);
    TextSurface->SetText("Dies");
    TextSurface->Blt(_mainSurface, 0.5, 0.3);

    if (m_StatisticsRecieved)
    {
        PlayersInfo players_info = m_PlayersInfo;
        std::map<int, PlayersInfo::Info>::iterator it;
        int pos = 0;
        for (it = players_info.m_Info.begin(); it != players_info.m_Info.end(); ++it)
        {
            std::pair<int, PlayersInfo::Info> pair = *it;
            PlayersInfo::Info info = pair.second;
            TextSurface->SetText(info.m_Name);
            TextSurface->Blt(_mainSurface, 0.1, 0.4 + pos * 0.1, GUI_Surface::AllignLeft, 0, 0);
            TextSurface->SetText(boost::lexical_cast<std::string>(info.m_Kills));
            TextSurface->Blt(_mainSurface, 0.4, 0.4 + pos * 0.1);
            TextSurface->SetText(boost::lexical_cast<std::string>(info.m_Dies));
            TextSurface->Blt(_mainSurface, 0.5, 0.4 + pos * 0.1);
            pos++;
        }
    }
    delete TextSurface;
    return true;
}


//////////////////////////////////////////////////////////////////////////


void GUI_Statistics::UpdateServerStatistics( const PlayersInfo& _playersInfo )
{
    m_PlayersInfo = _playersInfo;
    m_StatisticsRecieved = true;
}


//////////////////////////////////////////////////////////////////////////


void GUI_Statistics::Resize()
{
}


//////////////////////////////////////////////////////////////////////////


int GUI_Statistics::OnKeyChanged(Input* _input)
{
    int result = -1;
    Input::Key key = _input->GetKey(); 
    if (key.m_State == Input::RELEASED)
    {
        switch (key.m_Code)
        {
        case Input::TAB:
            result = Resume;
            break;
        }
    }
    return result;
}


//////////////////////////////////////////////////////////////////////////
